package com.example.memosystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ListView;

import androidx.annotation.AnimatorRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;


import com.example.memosystem.MainActivity;
import com.example.memosystem.R;
import com.example.memosystem.adapter.RecordAdapter;
import com.example.memosystem.bean.Record;
import com.example.memosystem.dao.impl.AdminDao;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
import java.util.List;

public class ManageActivity extends AppCompatActivity {

    ListView listView = null;
    List<Record> originalItems=null;
    RecordAdapter adapter = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);

        Intent intent = getIntent();
        String account = intent.getStringExtra("account");

        NavigationView navigationView = findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id = menuItem.getItemId();
                if(id == R.id.menu_quit)
                {
                    finishAffinity();//退出功能
                }
                if(id == R.id.menu_login)
                {
                    Intent intent = new Intent(ManageActivity.this, MainActivity.class);
                    startActivity(intent);
                }
                if(id == R.id.menu_pwd)
                {
                    Intent intent = new Intent(ManageActivity.this, ChangePwdActivity.class);
                    intent.putExtra("account", account);
                    startActivity(intent);
                }
                if(id == R.id.menu_mes)
                {
                    Intent intent = new Intent(ManageActivity.this, MesActivity.class);
                    intent.putExtra("account", account);
                    startActivity(intent);
                }

                return true;
            }
        });

        //添加事件点击按钮
        FloatingActionButton floatingActionButton = findViewById(R.id.manage_fab);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(ManageActivity.this, AddActivity.class);
                intent1.putExtra("account", account);
                startActivity(intent1);
            }
        });


        //开始加载列表
        listView = findViewById(R.id.manage_show_id);
        //获取数据
        originalItems = new ArrayList<>();
        originalItems = new AdminDao().showRecord(account);
        if(originalItems.isEmpty())
        {
            listView.setAdapter(null);
        }else {
            adapter = new RecordAdapter(this, originalItems);
            listView.setAdapter(adapter);
        }

        //菜单
        Toolbar toolbar = findViewById(R.id.toolbar_mange);
        this.setSupportActionBar(toolbar);

        DrawerLayout drawerLayout = findViewById(R.id.manage_drawer);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

    }
}